# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
  scipen = 999,
  digits = 16,
  max.print = .Machine$integer.max,
  show.error.locations = TRUE,
  warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------

library(data.table)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# READ IN OUR DATA -------------------------------------------------------------

# Our prizes; requires individual level data, not eligible for public release.

# To construct our baseline estimation sample, we impose three restrictions:
# 1)  We require each winner to be of working age (21 and 64) in their win year.
# 2)  We require each winner to be in the sample for at least two years prior to
#     generating their first Form W-2G.
# ==> This ensures that we observe pre-win economic outcomes for each individual
# 3) We restrict our baseline estimation sample to wins of 30K+
# ==> Choice translates to economically-meaningful $1K per year for avg winner.

our_sample_all_ages <-
    readRDS("~/summary-output/our-distributions.rds")
our_sample_all_ages <- setDT(our_sample_all_ages)

# Just need to impose restriction 1) from above^
# ==> the variable 'age_case' let's us do this
our_sample <- our_sample_all_ages[age_case == 1]

our_pretax_household <- our_sample$L_multiperiod_nonequiv_pretax
our_posttax_peradult <- our_sample$L_multiperiod
our_annuity_posttax_peradult <- our_sample$L_ann_multiperiod

# Housekeeping
our_sample_all_ages <- NULL
our_sample <- NULL
rm(our_sample_all_ages, our_sample)

# MAKE TABLE -------------------------------------------------------------------

filedir <- "~/paper/tables/table-A.1.tex"
file.create(filedir)

# Start Table 2.2
start_latex_table(column_structure = "lcccc", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "toprule", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

heading_text_input1 <-
    " & Pre-Tax Household Winnings & & \\multicolumn{2}{c}{Post-Tax Per-Adult Winnings}" #nolint
write_latex_table_row(
  heading_text_input1,
  file_dir = filedir,
  append = TRUE
)
add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 2,
    end_col = 2,
    file_dir = filedir,
    append = TRUE
)
add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 4,
    end_col = 5,
    file_dir = filedir,
    append = TRUE
)

heading_text_input2 <-
  " \\multirow{2}{*}{Statistic} & Size of win & & Size of win & Additional unearned annuity income"

heading_text_input3 <- " & (1) &  & (2) & (3)"
write_latex_table_row(heading_text_input2, file_dir = filedir, append = TRUE)
write_latex_table_row(heading_text_input3, file_dir = filedir, append = TRUE)

heading_text_input1 <- NULL
heading_text_input2 <- NULL
heading_text_input3 <- NULL
rm(heading_text_input1, heading_text_input2, heading_text_input3)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# Add body of Table 2.2
p01_row_input <-
  sprintf(
    "1st percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.01),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.01),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.01),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(p01_row_input, file_dir = filedir, append = TRUE)
p01_row_input <- NULL
rm(p01_row_input)

p05_row_input <-
  sprintf(
    "5th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.05),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.05),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.05),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(p05_row_input, file_dir = filedir, append = TRUE)
p05_row_input <- NULL
rm(p05_row_input)

p10_row_input <-
  sprintf(
    "10th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.10),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.10),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.10),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(p10_row_input, file_dir = filedir, append = TRUE)
p10_row_input <- NULL
rm(p10_row_input)

q1_row_input <-
  sprintf(
    "25th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.25),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.25),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.25),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(q1_row_input, file_dir = filedir, append = TRUE)
q1_row_input <- NULL
rm(q1_row_input)

median_row_input <-
  sprintf(
    "Median & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.50),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.50),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.50),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(median_row_input, file_dir = filedir, append = TRUE)
median_row_input <- NULL
rm(median_row_input)

mean_row_input <-
  sprintf(
    "Mean & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = mean(our_pretax_household),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = mean(our_posttax_peradult),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = mean(our_annuity_posttax_peradult),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(mean_row_input, file_dir = filedir, append = TRUE)
mean_row_input <- NULL
rm(mean_row_input)

q3_row_input <-
  sprintf(
    "75th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.75),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.75),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.75),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(q3_row_input, file_dir = filedir, append = TRUE)
q3_row_input <- NULL
rm(q3_row_input)

p90_row_input <-
  sprintf(
    "90th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.90),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.90),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.90),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(p90_row_input, file_dir = filedir, append = TRUE)
p90_row_input <- NULL
rm(p90_row_input)

p95_row_input <-
  sprintf(
    "95th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.95),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.95),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.95),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(p95_row_input, file_dir = filedir, append = TRUE)
p95_row_input <- NULL
rm(p95_row_input)

p99_row_input <-
  sprintf(
    "99th percentile & \\$%s & & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = quantile(our_pretax_household,
                       probs = 0.99),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_posttax_peradult,
                       probs = 0.99),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = quantile(our_annuity_posttax_peradult,
                       probs = 0.99),
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = -2,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(p99_row_input, file_dir = filedir, append = TRUE)
p99_row_input <- NULL
rm(p99_row_input)

# End Table 2.2
add_latex_table_rule(
  rule_type = "bottomrule",
  file_dir = filedir,
  append = TRUE
)
end_latex_table(file_dir = filedir, append = TRUE)
filedir <- NULL
rm(filedir)

# Housekeeping
our_pretax_household <- NULL
our_posttax_peradult <- NULL
our_annuity_posttax_peradult <- NULL
table_filename <- NULL
rm(
    our_pretax_household,
    our_posttax_peradult,
    our_annuity_posttax_peradult,
    table_filename
)